<script setup>
import { reactive, ref, watch, onMounted, getCurrentInstance, nextTick } from 'vue';
import { useIndexStore } from '../../pinia';
import { ElMessage } from "element-plus"
import { request } from '@/utils/axios/request.js'
import local from '@/components/local.vue'
import mitter from '../../utils/mitter';

let locabob = ref(false)
// 个人信息图片
let imgsU = ref('/images/img.jpg')

// 小眼睛
let showPassword = ref(false)
let store = useIndexStore()
let date = new Date().toLocaleString();
let username = ref('')
let tel = ref('')
let emilt = ref('')
let sex = ref('男性')
//确认密码弹窗
let dialogFormVisible = ref(false)
let form = reactive({
    password1: '',
    password2: '',
})
//重置
let resize = () => {
    username.value = ''
    tel.value = ''
    emilt.value = ''
    sex.value = '男性'
}
let usernameLoca = localStorage.getItem('username')
let userItem = ref({})
let findAll = null
if (usernameLoca) {
    findAll = async () => {
        try {
            locabob.value = true
            let newdata = await request({
                url: `/admin/user/users1?username=${usernameLoca}`,
                method: 'get'
            })
            newdata.data.forEach(item => {
                item.role = item.role == 0 ? '超级管理员' : item.role == 1 ? '普通管理员' : '普通员工'
                item.status = item.status == 1 ? '正常' : '停用'
            })
            userItem.value = newdata.data[0]
            imgsU.value = userItem.value.url
        } catch (err) {
            console.log(err);
        }
        locabob.value = false
    }
    findAll()
}
watch(imgsU, (newV) => {
    mitter.emit('imgUrl', newV)
})

//确认密码
let newsave = ref('')
let passave = async () => {
    let userItems = ref('')
    let usernameLoca = localStorage.getItem('username')
    if (usernameLoca) {
        try {
            let newdata = await request({
                url: `/admin/user/users1?username=${usernameLoca}`,
                method: 'get'
            })
            userItems.value = newdata.data[0]
        } catch (err) {
            console.log(err);
        }
    }
    if (form.password1 != userItems.value.password) {
        newsave.value = "原始密码错误"
        ElMessage.error('原始密码错误')
        form.password1 = ''
        form.password2 = ''
        return;
    } else if (form.password2 != userItems.value.password) {
        newsave.value = "确认密码错误"
        ElMessage.error('确认密码错误')
        form.password2 = ''
        return;
    } else {
        newsave.value = "确认密码成功"
        ElMessage.success('确认密码成功')
        dialogFormVisible.value = false
        form.password1 = ''
        form.password2 = ''
        return;
    }
}

// 图片上传
let filed = null;
let that = getCurrentInstance()
onMounted(() => {
    filed = async () => {
        if (that.refs.filesed.files.length == 0) {
            ElMessage.warning('您未上传图片')
            return
        }
        let file = that.refs.filesed.files[0]
        if (!/\.(jpg|png|JPG|PNG|gif|webp|jpeg)$/.test(file.name)) {
            ElMessage.warning('图片格式错误')
            return
        }
        if (file.size >= 8192000) {
            ElMessage.warning('图片大小必须小于8M')
            return
        }
        const formData = new FormData();
        formData.append('avatar', file);
        try {
            locabob.value = true
            let imgUrl = await request({
                url: '/admin/imgUrl',
                method: 'post',
                data: formData,
                headers: { 'content-type': 'multipart/form-data' }
            })
            let data = await request({
                url: '/admin/user/users',
                method: 'put',
                data: {
                    newTableDate: { url: imgUrl.url },
                    id: userItem.value.id
                }
            })
            imgsU.value = imgUrl.url
        } catch (error) {
            console.log(error);
        }
        locabob.value = false
    }
})
//确认密码成功后保存
const save = async () => {
    if (username.value == '' || tel.value == '' || emilt.value == '') {
        ElMessage.error('信息不能为空')
        return;
    }
    if (!/^[a-zA-Z0-9\u4e00-\u9fa5]+$/.test(username.value)) {
        ElMessage.error('姓名不规范')
        username.value = ''
        return;
    }
    if (!/^(0\d{2,3}-\d{7,8}(-\d{3,4})?|1[3456789]\d{9})$/.test(tel.value)) {
        ElMessage.error('号码格式错误')
        tel.value = ''
        return;
    }
    if (!/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(emilt.value)) {
        ElMessage.error('邮箱格式错误')
        emilt.value = ''
        return;
    }
    if (newsave.value == "确认密码错误" || newsave.value == "原始密码错误" || newsave.value == "") {
        dialogFormVisible.value = true;
        return;
    } else if (newsave.value == "确认密码成功") {
        let obj = {}
        obj.nickname = username.value
        obj.phone_number = tel.value
        obj.emil = emilt.value
        obj.sex = sex.value
        try {
            locabob.value = true
            let data = await request({
                url: '/admin/user/users',
                method: 'put',
                data: {
                    newTableDate: obj,
                    id: userItem.value.id
                }
            })
            findAll()
            ElMessage.success(data.message)
        } catch (err) {
            console.log(err);
        }
        locabob.value = false
        resize()
        newsave.value = ""
    }
}

</script>

<template>
    <div class="right">
        <local v-show="locabob" />
        <div class="right-first">
            <div class="title">基本信息</div>
            <div class="img">
                <label for="files">
                    <img class="img-tp" :src="imgsU" alt="" />
                </label>
            </div>
            <input type="file" style="display: none;" id="files" @change="filed" ref="filesed">
            <ul>
                <li class="middle">
                    <span>姓名</span>
                    <span>{{ userItem.nickname }}</span>
                </li>
                <li class="middle">
                    <span>手机号码</span>
                    <span>{{ userItem.phone_number }}</span>
                </li>
                <li class="middle">
                    <span>邮箱</span>
                    <span>{{ userItem.emil }}</span>
                </li>
                <li class="middle">
                    <span>归属部门</span>
                    <span>{{ userItem.department }}</span>
                </li>
                <li class="middle">
                    <span>角色</span>
                    <span>{{ userItem.role }}</span>
                </li>
                <li class="middle">
                    <span>创建时间</span>
                    <span>{{ userItem.created_at }}</span>
                </li>
            </ul>
        </div>
        <div class="right-last">
            <div class="title">用户信息修改</div>
            <div class="details">
                <div class="mes">姓名：<el-input class="color" v-model="username" placeholder="姓名"
                        style="width: calc(100% - 100px);"></el-input></div>
                <div class="mes">手机号码：<el-input class="color" v-model="tel" placeholder="请输入手机号码"
                        style="width: calc(100% - 100px);"></el-input></div>
                <div class="mes">邮箱：<el-input class="color" v-model="emilt" placeholder="请输入邮箱"
                        style="width: calc(100% - 100px);"></el-input></div>
                <div class="mess">
                    <span> 性别:</span>
                    <label><input type="radio" v-model="sex" name="gender" value="男" checked class="sex"
                            style="margin-left: 10px;">&nbsp;男&nbsp;&nbsp;</label>
                    <label><input type="radio" v-model="sex" name="gender" value="女">&nbsp;女&nbsp;&nbsp;</label>
                </div>
                <el-row class="mes">
                    <div>
                        <el-button type="primary" @click="save">保存</el-button>
                        <el-button @click="resize">重置</el-button>
                    </div>
                </el-row>
            </div>
        </div>
        <div class="popbox">
            <el-dialog v-model="dialogFormVisible" title="确认密码">
                <el-form :model="form">
                    <el-form-item label="原始密码" label-width="140px">
                        <el-input v-model="form.password1" type="password" autocomplete="off" :show-password="true" />
                        <i class="el-icon-view el-input__icon" slot="suffix" @click="showPassword = !showPassword"></i>
                    </el-form-item>
                    <el-form-item label="确认密码" label-width="140px">
                        <el-input v-model="form.password2" type="password" autocomplete="off" :show-password="true" />
                        <i class="el-icon-view el-input__icon" slot="suffix" @click="showPassword = !showPassword"></i>
                    </el-form-item>
                </el-form>
                <template #footer>
                    <span class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="passave">
                            确认
                        </el-button>
                    </span>
                </template>
            </el-dialog>
        </div>
    </div>
</template>

<style lang="less" scoped>
.right {
    display: flex;
    margin-left: 25px;
    width: 100%;
    padding-right: 16px;

    .right-first {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
        color: #000000d9;
        font-size: 14px;
        line-height: 1.5715;
        list-style: none;
        position: relative;
        background: #fff;
        border-radius: 2px;
        width: 30%;
        border: 1px solid rgb(240, 240, 240);

        .title {
            display: flex;
            padding: 16px 0 5px 32px;
            font-size: 16px;
            border-bottom: 1px solid rgb(240, 240, 240);
        }

        .img {
            margin-top: 20px;
            width: 200px;
            height: 200px;
            position: relative;
            left: 50%;
            transform: translateX(-50%);

            .img-tp {
                // width: 12.5rem;
                // height: 12.5rem;
                width: 100%;
                height: 100%;
                border-radius: 50%;
                display: block;
            }
        }

        .middle {
            display: flex;
            padding-bottom: .5rem;
            padding-top: .5rem;
            justify-content: space-between;
            padding: 10px;
            border-bottom: 1px solid rgb(240, 240, 240);
        }
    }

    .right-last {
        width: 68%;
        margin-left: 25px;
        padding-left: 12px;
        border: 1px solid rgb(240, 240, 240);
        box-sizing: content-box;

        .title {
            display: flex;
            padding: 16px 0 10px 32px;
            font-size: 16px;
            border-bottom: 1px solid rgb(240, 240, 240);
        }

        .details {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            color: #000000d9;
            font-size: 14px;
            line-height: 1.5715;
            width: 100%;

            .mes {
                display: flex;
                padding: 16px 20px 10px 32px;
                border-radius: 4px;
                font-weight: 700;
                justify-content: space-between;
                ;

            }

            .mess {
                display: flex;
                padding: 16px 20px 10px 32px;
                border-radius: 4px;
                font-weight: 700;

                span {
                    margin-right: 6%;
                }
            }
        }
    }

    :deep .el-input {
        width: 300px;
    }

    .dialog-footer button:first-child {
        margin-right: 10px;
    }

    :deep .el-form {
        margin-left: 50px;
    }

}
</style>